//
//  MeatOrderLocationCell.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/26.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatOrderLocationCell.h"

@interface MeatOrderLocationCell ()

/**
 默认
 */
@property (nonatomic, strong) UILabel *defaultLab;


/**
 城市
 */
@property (nonatomic, strong) UILabel *cityLab;


/**
 创业总部基地B区12号楼1208室
 */
@property (nonatomic, strong) UILabel *locationLab;


/**
 傅小小/电话
 */
@property (nonatomic, strong) UILabel *nameLab;





@end


@implementation MeatOrderLocationCell

#pragma mark - lifecycle methods

- (void)setupUI{
    
    _type = MeatOrderLocationCellType_None;
    
    [self addSubview:self.defaultLab];
    [self addSubview:self.cityLab];
    [self addSubview:self.locationLab];
    [self addSubview:self.nameLab];
    [self addSubview:self.arrowBtn];
    
}

- (void)setupLayout{
    
    CGFloat width = KScreenWidth - 30;
    CGFloat height = 90;
    
    {
        _defaultLab.size = CGSizeMake(50, 16);
        _defaultLab.mj_x = _defaultLab.mj_y = 15;
    }
    
    
    {
        _cityLab.mj_x = _defaultLab.right + 5;
        _cityLab.mj_y = _defaultLab.mj_y;
        _cityLab.height = _defaultLab.height;
        _cityLab.width = width - _cityLab.left - 40;
    }
    
    
    {
        _locationLab.mj_x = 15;
        _locationLab.height = 22;
        _locationLab.mj_y = _cityLab.bottom +4;
        _locationLab.width = width - 55;
    }
    
    {
        _nameLab.mj_x = 15;
        _nameLab.mj_y = _locationLab.bottom + 4;
        _nameLab.width = _locationLab.width;
        _nameLab.height = 17;
    }
    
    {
        _arrowBtn.size = CGSizeMake(40, 40);
        _arrowBtn.mj_y = (height - _arrowBtn.height)/2.0;
        _arrowBtn.mj_x = width - _arrowBtn.width;
    }
    
    switch (self.type) {
        case MeatOrderLocationCellType_None:
            {
                _defaultLab.hidden = YES;
                _nameLab.hidden = YES;
                _cityLab.hidden = YES;
                
                NSMutableAttributedString *aims =  [@"  选择送货地址" createAttributingByIcon:@"address_tip" downIndexImage:nil textColor:UIColorFromRGB(0x333333) font:mFont(17)];
                _locationLab.attributedText = aims;

            }
            break;
            
        default:
            {
                _defaultLab.hidden = NO;
                _nameLab.hidden = NO;
                _cityLab.hidden = NO;
                
                Address *aimAddress = nil;
                for (NSInteger k = 0; k< self.locations.count; k++) {
                    
                    Address *address = self.locations[k];
                    if ([address.isDefault integerValue] == 1) {
                        
                        aimAddress = address;
                        break;
                    }
                }
                
                if (!aimAddress) {
                    aimAddress = self.locations[0];
                }
                
                if (self.addressBlock) {
                    self.addressBlock(aimAddress);
                }
                
                if ([aimAddress.isDefault integerValue] == 1) {
                    _defaultLab.text = @"默认";
                    _defaultLab.width = 40;
                    _cityLab.mj_x = _defaultLab.right +5;
                    
                }else{
                    _defaultLab.width = 0;
                    _defaultLab.text = @"";
                    _cityLab.mj_x = _defaultLab.right;
                }
                
                _nameLab.text = [NSString stringWithFormat:@"%@  %@", aimAddress.userName, aimAddress.phone];
                _cityLab.text  = aimAddress.localName;
                _locationLab.text = aimAddress.details;
                _locationLab.textColor = UIColorFromRGB(0x6262626);
                
            }
            break;
    }
    
}

#pragma mark - public methods

#pragma mark - setter methods
- (void)setLocations:(NSArray *)locations{
    
    _locations = locations;
    self.type = locations.count >0 ?MeatOrderLocationCellType_Have :MeatOrderLocationCellType_None;
    [self setupLayout];
}

- (void)setLocationModel:(MeatOrderDetailGoodOrderModel *)locationModel{
    
    _defaultLab.hidden = NO;
    _nameLab.hidden = NO;
    _cityLab.hidden = NO;
    
    
    if ([locationModel.addressDefaultIf integerValue] == 1) {
        _defaultLab.text = @"默认";
        _defaultLab.width = 40;
        _cityLab.mj_x = _defaultLab.right +5;
        
    }else{
        _defaultLab.width = 0;
        _defaultLab.text = @"";
        _cityLab.mj_x = _defaultLab.right;
    }
    
    /*
     addressTag = "家";
     cancelReason = <nil>;
     cancelTime = <nil>;
     createTime = "2019-09-05 12:24:51";
     evaluateTime = <nil>;
     goNum = "1213811141120190905122450";
     goStatus = 2;
     goType = 4;
     goodsOrderId = 98;
     memberId = 61;
     orderClass = 0;
     payAmount = 0.05;
     payTime = "2019-09-05 12:24:55";
     payType = "1";
     pigId = 23;
     receiveAddress = "河北省廊坊市广阳区和平路81号甲110";
     receiveName = "王旭";
     receivePhone = "15022632138";
     receiveRegion = "河北省廊坊市广阳区和平路81号甲";
     receiveTime = <nil>;
     refundIf = <nil>;
     remark = "";
     sendTime = <nil>;
     shopId = 2;
     shopName = "店铺2";
     */
    
    _nameLab.text = [NSString stringWithFormat:@"%@  %@", locationModel.receiveName, locationModel.receivePhone];
    _cityLab.text  = locationModel.receiveRegion;
    _locationLab.text = locationModel.receiveAddress;
    _locationLab.textColor = UIColorFromRGB(0x6262626);
    
    
}


#pragma mark - getter methods

- (UILabel *)defaultLab{
    
    if (!_defaultLab) {
        _defaultLab = [UILabel makeLabelWithText:@"" textColor:KWhiteColor font:mFont(11) textAliment:1];
        [self addShadowAndCircleCorner:_defaultLab.layer corner:8];
        _defaultLab.backgroundColor = UIColorFromRGB(0xFF8A00);
    }
    return _defaultLab;
}


- (UILabel *)cityLab{
    
    if (!_cityLab) {
        _cityLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x666666) font:mFont(12) textAliment:0];
    }
    return _cityLab;
}


- (UILabel *)locationLab{
    
    if (!_locationLab) {
        _locationLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x6262626) font:mFont(16) textAliment:0];
    }
    return _locationLab;
}


- (UILabel *)nameLab{
    
    if (!_nameLab) {
        _nameLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x808080) font:mFont(12) textAliment:0];
    }
    return _nameLab;
}


- (UIButton *)arrowBtn{
    
    if (!_arrowBtn) {
        _arrowBtn = [UIButton makeButtonWithImageName:@"meat_cart_enter"];
        _arrowBtn.contentMode = UIControlContentHorizontalAlignmentRight;
        
    }
    return _arrowBtn;
}


@end
